#include <linux/init.h>
#include <linux/sched.h>
#include <linux/printk.h>
#include <linux/sched/task.h>
#include <linux/delay.h>
#include <linux/mutex.h>

static DEFINE_MUTEX(_mutex);

static int fair_task(void *arg)
{
    struct task_struct *self = current;

    printk("fair-task\n");

    while (1)
    {
        mutex_lock(&_mutex);
        printk("F1 pri:%d\n", self->prio);
        msleep(2000);
        printk("F2 pri:%d\n", self->prio);
        mutex_unlock(&_mutex);
    }

    return 0;
}

static int rt_task(void *p)
{
    const struct sched_param param = {50};
    struct task_struct *self = current;

    sched_setscheduler_nocheck(current, SCHED_RR, &param);

    printk("rt-task\n");

    msleep(1000);

    printk("R0 pri:%d\n", self->prio);

    while (1)
    {
        mutex_lock(&_mutex);
        printk("R1 pri:%d\n", self->prio);
        msleep(1000);
        printk("R2 pri:%d\n", self->prio);
        mutex_unlock(&_mutex);
        msleep(1010);
    }

    return 0;
}

static int testtask_init(void)
{
    kernel_thread(rt_task, NULL, "RT", 0);
    kernel_thread(fair_task, NULL, "FT", 0);

    return 0;
}
late_initcall(testtask_init);
